/**
 * 
 */
package com.lijin.workshop.dao;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.lijin.workshop.dao.mybatis.mapper.DataDicMapper;
import com.lijin.workshop.dao.mybatis.model.DataDic;
import com.lijin.workshop.dao.mybatis.model.DataDicExample;
import com.lijin.workshop.dao.mybatis.model.DataDicExample.Criteria;
import com.lijin.workshop.entity.Pager;
import com.lijin.workshop.util.StringUtil;

/**
 * @author jin
 *
 */
@Component
public class DataDicDAO extends BaseDAO {

	/**
	 * 根据查询条件分页查询数据字典信息，
	 * @param pager 分页数据
	 * @param qryParamMap 查询条件
	 * @return
	 */
	public Pager<DataDic> selectDataDicByPage(Pager<DataDic> pager, Map<String, String> qryParamMap) {
		// 构造查询条件
		DataDicExample example = this.getQryExample(qryParamMap);
		DataDicMapper mapper = this.getMapper(DataDicMapper.class);
		// 查询总数
		pager.setTotal(mapper.countByExample(example));
		// 构造分页信息
		example.setStartNum(pager.getStartNum());
		example.setPageSize(pager.getPageSize());
		// 将查询结果放入分页对象
		List<DataDic> lst = mapper.selectByExample(example);
		pager.setResultList(lst);
		return pager;
	}
	
	/**
	 * 根据查询条件查询复合条件的记录
	 * @param qryParamMap
	 * @return
	 */
	public List<DataDic> selectDataDic(Map<String, String> qryParamMap) {
		// 构造查询条件
		DataDicExample example = this.getQryExample(qryParamMap);
		DataDicMapper mapper = this.getMapper(DataDicMapper.class);
		return mapper.selectByExample(example);
	}
	
	/**
	 * 新增数据字典
	 * @param dataDic
	 * @return
	 */
	public void addDataDic(DataDic dataDic) {
		DataDicMapper mapper = this.getMapper(DataDicMapper.class);
		mapper.insert(dataDic);
	}
	
	/**
	 * 修改数据字典
	 * @param dataDic
	 * @return
	 */
	public void updateDataDic(DataDic dataDic) {
		DataDicMapper mapper = this.getMapper(DataDicMapper.class);
		mapper.updateByPrimaryKey(dataDic);
	}
	
	/**
	 * 根据传入的查询条件Map构造查询条件
	 * @param qryParamMap
	 * @return
	 */
	@Override
	protected DataDicExample buildQryExample(Map<String, String> qryParamMap) {
		DataDicExample example = new DataDicExample();
		if (qryParamMap != null && !qryParamMap.isEmpty()) {
			Criteria c = example.createCriteria();
			if (qryParamMap.get("dataTp") != null) {
				String dataTp = qryParamMap.get("dataTp");
				// 如果dataTp不是空,则用dataTp做模糊查询条件
				if (!StringUtil.isBlank(dataTp)) {
					c.andDataTpLike("%" + StringUtil.trim(dataTp) + "%");
				}
			}
			if (qryParamMap.get("dataKey") != null) {
				String dataKey = qryParamMap.get("dataKey");
				// 如果dataKey不是空,则用dataTp做模糊查询条件
				if (!StringUtil.isBlank(dataKey)) {
					c.andDataKeyLike("%" + StringUtil.trim(dataKey) + "%");
				}
			}
		}
		// 排序字段
		example.setOrderByClause("data_tp asc, sort_index asc");
		return example;
	}
}
